package com.alm.web.controller.alm.statisticalexport;

import com.alm.common.annotation.Log;
import com.alm.common.constant.UserConstants;
import com.alm.common.core.controller.BaseController;
import com.alm.common.core.domain.AjaxResult;
import com.alm.common.core.domain.entity.SysMenu;
import com.alm.common.core.domain.entity.SysUser;
import com.alm.common.core.page.TableDataInfo;
import com.alm.common.enums.BusinessType;
import com.alm.common.utils.SecurityUtils;
import com.alm.common.utils.ServletUtils;
import com.alm.handle.core.domain.entity.SysAreaInfo;
import com.alm.handle.core.domain.entity.SysDevLoginLog;
import com.alm.handle.core.domain.entity.rq.DevLoginLogRq;
import com.alm.handle.service.ISysAreaInfoService;
import com.alm.handle.service.ISysDevLoginLogService;
import com.alm.system.service.ISysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/alm/dev/statisticalexport")
public class AlmAreaManageController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysAreaInfoService areaInfoService;

    @Autowired
    private ISysDevLoginLogService devLoginLogService;

//    @PreAuthorize("@ss.hasPermi('alm:dev:statisticalexport:area')")
    @PreAuthorize("@ss.hasRole('common')")
    @GetMapping("/area")
    public AjaxResult getAreaList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysAreaInfo> areaInfos = areaInfoService.selectAreaList(user.getDeptId());
        return success(areaInfos);
    }

//    @PreAuthorize("@ss.hasPermi('alm:dev:statisticalexport:query')")
    @PreAuthorize("@ss.hasRole('common')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getAreaInfo(@PathVariable Long menuId) {
        SysAreaInfo sysAreaInfo = areaInfoService.selectAreaInfoById(menuId);
        return success(sysAreaInfo);
    }

    /**
     * 删除菜单
     */
//    @PreAuthorize("@ss.hasPermi('alm:dev:statisticalexport:remove')")
    @PreAuthorize("@ss.hasRole('common')")
    @Log(title = "区域管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        if (areaInfoService.hasChildByAreaId(menuId)) {
            return warn("存在子菜单,不允许删除");
        }
        return toAjax(areaInfoService.deleteAreaById(menuId));
    }

    /**
     * 新增菜单
     */
//    @PreAuthorize("@ss.hasPermi('alm:dev:statisticalexport:add')")
    @PreAuthorize("@ss.hasRole('common')")
    @Log(title = "区域管理", businessType = BusinessType.INSERT)
    @PostMapping("/area")
    public AjaxResult add(@Validated @RequestBody SysAreaInfo area) {
        if (!areaInfoService.checkMenuNameUnique(area)) {
            return error("新增区域'" + area.getMenuName() + "'失败，区域名称已存在");
        }
        area.setCreateBy(SecurityUtils.getLoginUser().getUser().getDeptId());
        return toAjax(areaInfoService.insertArea(area));
    }

    /**
     * 修改菜单
     */
//    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @PreAuthorize("@ss.hasRole('common')")
    @Log(title = "区域管理", businessType = BusinessType.UPDATE)
    @PutMapping("/area")
    public AjaxResult edit(@Validated @RequestBody SysAreaInfo area) {
        if (!areaInfoService.checkMenuNameUnique(area)) {
            return error("新增区域'" + area.getMenuName() + "'失败，区域名称已存在");
        } else if (area.getMenuId().equals(area.getParentId())) {
            return error("修改区域'" + area.getMenuName() + "'失败，上级区域不能选择自己");
        }
        area.setUpdateBy(getUsername());
        return toAjax(areaInfoService.updateArea(area));
    }

    /**
     * 查询设备登录日志
     */
    @PreAuthorize("@ss.hasRole('common')")
    @PostMapping("/getDevLoginLog")
    public TableDataInfo getDevLoginLog(@RequestBody DevLoginLogRq rq) {
        startPage(rq.getPageNum(), rq.getPageSize());
        String devMac = StringUtils.isNotBlank(rq.getDevMac()) ? rq.getDevMac() : "";
        String routerMac = StringUtils.isNotBlank(rq.getRouterMac()) ? rq.getDevMac() : "";
        List<SysDevLoginLog> devLoginLogs =  devLoginLogService.getDevLoginLog(devMac, routerMac);
        return getDataTable(devLoginLogs);
    }

}
